import http from "@/api/api";
import fnBlockLoading from "@/utils/loading";

export const codeDate = async function (path) {
    let resultPath = "";
    resultPath = path.split("/")[1];
    resultPath = "/" + resultPath;
    let STATE_CACHE = localStorage.getItem("menuListcode");
    let MENU = [];
    STATE_CACHE = (STATE_CACHE && JSON.parse(STATE_CACHE)) || [];
    if (path !== "/" && path !== "/layout" && STATE_CACHE.length == 0) {
        await fnBlockLoading(async () => {
            MENU = await http.post("/provPc-web/userHome/menuList");
            MENU.forEach((sourceItem) => {
                sourceItem.resource?.forEach((item) => MENU.push(item));
            });
            localStorage.setItem("menuListcode", JSON.stringify(MENU));
        });
    } else {
        MENU = STATE_CACHE;
    }
    let list = "";
    MENU.find((item) => {
        if (item.menuUrl == resultPath) {
            list = item.code || "";
        }
    });
    return list;
};
